#include "inputhandler.h"

#include <iostream>

#include "renderer/renderer.h"

#include "sim/simbase.h"

#include "gamestate.h"
#include "link.h"
#include "module.h"

InputHandler* inputHandler;

//static
void InputHandler::MouseClick(int button, int action){
    int xpos;
    int ypos;
    glfwGetMousePos(&xpos, &ypos);
    if(button==0 and action==1){
        int picked = renderer->Pick(xpos, ypos);
        inputHandler->ProcessClick(picked);
    }
}

void InputHandler::ProcessClick(unsigned int picked){
    Module* module;
    Link* link;

    module = gamestate->FindModule(picked);
    if(module){
        std::cout<<"Module clicked\n";
        return;
    }

    link = gamestate->FindLink(picked);
    if(link){
        std::cout<<"Link clicked\n";
        Module* parent = gamestate->FindLinkParentModule(picked);
        switch(mode){
            case BUILD:{
                std::cout<<"Build\n";
                Math::PrintVector(link->Heading());
                Module* child = gamestate->CreateModule();
                parent->AttachModule(child, link);

                break;
            }
            default:{
                break;
            }
        }
        return;
    }

}
